package com.starlight.flower_server.controller;

import com.starlight.flower_server.controller.authentication.AuthenticationCheck;
import com.starlight.flower_server.controller.exception.BadRequestException;
import com.starlight.flower_server.domain.cart.cqrs.CartAddItemCommand;
import com.starlight.flower_server.domain.cart.data.CartData;
import com.starlight.flower_server.domain.order.data.OrderData;
import com.starlight.flower_server.domain.service.OrderService;
import com.starlight.flower_server.domain.service.cart.CartService;
import com.starlight.flower_server.infrastructure.FlowerServerResponse;
import com.starlight.flower_server.infrastructure.RoleType;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Order;
import com.starlight.flower_server.infrastructure.entity.cart.Cart;
import com.starlight.flower_server.infrastructure.entity.cart.CartItem;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author yfy2001
 * @date 2023/5/19 12:46
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @Resource
    private OrderService orderService;

    /**
     * 查看购物车详情
     * @return cartData
     * private Date created;
     * private Map<Long, CartItem> itemMap;包含购物车中所有商品项的map
     */
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    @GetMapping("/getCartDetail")
    public FlowerServerResponse<CartData> getCartDetail(){
        return FlowerServerResponse.success(cartService.getDetail());
    }

    /**
     * 加入购物车
     * @param command
     * private Long itemId;
     * private String itemName;
     * private String number;
     * private String image;
     * @return
     */
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    @PostMapping("/addItemToCart")
    public FlowerServerResponse<Boolean> addItemToCart(CartAddItemCommand command){
        CartItem cartItem=new CartItem();
        cartItem.setItemId(command.getItemId());
        cartItem.setItemName(cartItem.getItemName());
        cartItem.setNumber(command.getNumber());
        cartItem.setImage(cartItem.getImage());
        return FlowerServerResponse.success(cartService.addItem(cartItem));
    }

    /**
     * 移除商品
     * @param itemId
     * @return
     */
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    @PostMapping("/removeItemFromCart")
    public FlowerServerResponse<Boolean> removeItemFromCart(Long itemId){
        return FlowerServerResponse.success(cartService.removeItem(itemId));
    }

    /**
     * 结算
     * @return
     * 返回购物车基本信息
     * 返回购物车总价
     * 返回生成的订单列表
     */
    @PostMapping("/settleCart")
    @AuthenticationCheck(roleType = {RoleType.ROOT,RoleType.NORMAL,RoleType.PRIMARY,RoleType.SENIOR})
    public FlowerServerResponse<CartData> settleCart(List<Long> itemIdList){
        Cart cart=cartService.getCart();
        BigDecimal totalPrice=new BigDecimal(0);
        Map<Long,CartItem> cartItemMap=cart.getItemMap();
        Map<Long,CartItem> selectedItemMap=cartItemMap.entrySet()
                .stream().filter(longCartItemEntry
                        -> itemIdList.contains(longCartItemEntry.getKey())
                ).collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue));
        //订单id列表
        List<Long> orderIdList=new ArrayList<>();
        for (CartItem cartItem:selectedItemMap.values()) {
            Order order=new Order();
            order.setItemId(cartItem.getItemId());
            order.setNumber(cartItem.getNumber());
            orderService.insertOrder(order);
            totalPrice=totalPrice.add(order.getTotalCharge());
            orderIdList.add(order.getId());
        }
        CartData cartData=new CartData();
        BeanUtils.copyProperties(cart,cartData);
        cartData.setTotalPrice(totalPrice);
        List<OrderData> orderDataList=orderService.queryOrderListByIdList(orderIdList);
        cartData.setOrderDataList(orderDataList);
        return FlowerServerResponse.success(cartData);
    }

    @PostMapping("/clearCart")
    public FlowerServerResponse<Boolean> clearCart(){
        Cart cart=cartService.createCart();
        if (cart==null){
            throw new BadRequestException(Status.CART_CREATE_FAILED.getMessage(),Status.CART_CREATE_FAILED);
        }
        return FlowerServerResponse.success(true);
    }

}
